//
//  ZXOpenMapsManager.m
//  MerchantBusinessClient
//
//  Created by simon on 2020/4/29.
//  Copyright © 2020 com.Chs. All rights reserved.
//

#import "ZXOpenMapsManager.h"
#import "ZXOpenMapsSchemeManager.h"

@interface ZXOpenMapsManager ()

@end


@implementation ZXOpenMapsManager

- (void)dealloc{
    
}


+ (UIAlertController *)showActionSheetInViewController:(UIViewController *)viewController
                                          withLatitude:(double)lat
                                             longitude:(double)lon
                                               poiName:(nullable NSString *)poiName
                                              tapBlock:(nullable void (^)(UIAlertController * _Nonnull alertController, UIAlertAction * _Nonnull action, NSInteger buttonIndex))tapBlock
{
    ZXOpenMapsManager *manager = [[ZXOpenMapsManager alloc] init];
    return [manager showActionSheetInViewController:viewController withLatitude:lat longitude:lon poiName:poiName tapBlock:tapBlock];
}

- (UIAlertController *)showActionSheetInViewController:(UIViewController *)viewController
                                          withLatitude:(double)lat
                                             longitude:(double)lon
                                               poiName:(nullable NSString *)poiName
                                              tapBlock:(nullable void (^)(UIAlertController * _Nonnull alertController, UIAlertAction * _Nonnull action, NSInteger buttonIndex))tapBlock
{
    ZXOpenMapsSchemeManager *schemeManager = [ZXOpenMapsSchemeManager new];
    //获取支持的导航项集合
    NSArray *mItems = [schemeManager getSupportMapItemSchemes];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    if (mItems.count>0)
    {
        for(NSUInteger i=0; i<mItems.count; i++)
        {
            NSDictionary *dic = [mItems objectAtIndex:i];
            NSString *otherButtonTitle = dic[@"title"];
            if (otherButtonTitle.length >0) {
                UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle
                                                                      style:UIAlertActionStyleDefault
                                                                    handler:^(UIAlertAction *action){
                    ZXMapType type = [[dic objectForKey:@"mapType"] integerValue];
                    if (type == ZXMapTypeAMap)
                    {
                        [ZXOpenMapsSchemeManager openSchemeURLToAMapClientForNavigationWithLatitude:lat longitude:lon poiName:poiName openFailure:nil];
                    }
                    else if (type == ZXMapTypeApple)
                    {
                        [ZXOpenMapsSchemeManager openSchemeURLToAppleMapClientForNavigationWithLatitude:lat longitude:lon poiName:poiName];
                    }
                    else if (type == ZXMapTypeBaidu)
                    {
                        [ZXOpenMapsSchemeManager openSchemeURLToBaiduMapClientForNavigationWithLatitude:lat longitude:lon poiName:poiName openFailure:nil];
                    }
                    if (tapBlock) {
                        tapBlock(alertController, action,i);
                    }
                }];
                [alertController addAction:otherAction];
            }
        }
        NSString *cancelButtonTitle = @"取消";
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action){
                                                                    if (tapBlock)
                                                                    {
                                                                        tapBlock(alertController,action,-1);
                                                                    }
                                                             }];
        [alertController addAction:cancelAction];
    }else{
        ///苹果系统其实不会发生，因为始终会有苹果地图。
        alertController.title = @"您的手机没有安装苹果、高德或者百度导航，快去下载吧";
        NSString *cancelButtonTitle = @"确定";
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle
                                                               style:UIAlertActionStyleCancel
                                                             handler:^(UIAlertAction * _Nonnull action){
                                                                    if (tapBlock)
                                                                    {
                                                                        tapBlock(alertController,action,-1);
                                                                    }
                                                             }];
        [alertController addAction:cancelAction];
    }
    [viewController presentViewController:alertController animated:YES completion:nil];
    return alertController;
}




// 百度地图BD09LL经纬度转换为高德（GCJ02）经纬度
+ (CLLocationCoordinate2D)getGaoDeCoordinateByBaiDuCoordinate:(CLLocationCoordinate2D)coordinate
{
//    return CLLocationCoordinate2DMake(coordinate.latitude - 0.006, coordinate.longitude - 0.0065);
    double X_PI = M_PI * 3000.0 / 180.0;
    double x = coordinate.longitude - 0.0065;
    double y = coordinate.latitude - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * X_PI);
    double theta = atan2(y, x) - 0.000003 * cos(x * X_PI);
    double lat = z * sin(theta);
    double lon = z * cos(theta);
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(lat, lon);
    return coor;
}

// 高德地图火星坐标系（GCJ02）经纬度转换为百度BD09LL经纬度
+ (CLLocationCoordinate2D)getBD09CoordinateByGaoDeCoordinate:(CLLocationCoordinate2D)coordinate
{
//    return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065);
    double X_PI = M_PI * 3000.0 / 180.0;
    double x = coordinate.longitude;
    double y = coordinate.latitude;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * X_PI);
    double theta = atan2(y, x) + 0.000003 * cos(x * X_PI);
    double lon = z * cos(theta) + 0.0065;
    double lat = z * sin(theta) + 0.006;
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(lat, lon);
    return coor;
}


@end
